﻿using PSL.Lexer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSL.Shell
{
    class Program
    {
        private static bool runShell;

        static void Main(string[] args)
        {
            Console.Write(new String('-', 80));
            Console.WriteLine(String.Format("PrivateScriptLanguage - {0}", InternalAssemblyVersion.Version));
            Console.WriteLine("Excuting in .Net " + System.Environment.Version.ToString());
            Console.WriteLine("Close the application with the 'exit' command");
            Console.WriteLine(new String('-', 80));

            // Definition and declarations
            runShell = true;

            // Create PSL
            PSLRuntime runtime = new PSLRuntime();

            // Set Failure-Output
            //host.FuncNotValidEventHandler += PSLHostFuncNotValidEventHandler;

            // Include Packages
            PSL.Package.System.PackageManager.Singleton.SetupPackages(runtime, Package.System.Packages.All);

            Action actDelegate = ExitShell;
            runtime.AddMethod("exit", actDelegate);

            runtime.Execute(System.IO.File.ReadAllText(@"C:\Users\beggers\Documents\Visual Studio 2013\Projects\PrivateScriptLanguage\PrivateScriptLanguage.Shell\bin\Debug\Script\SampleScript.psl"));

            string currentCommand = "";
            while (runShell)
            {
                Console.Write("> ");
                currentCommand = Console.ReadLine();

                try
                {
                    runtime.Execute(currentCommand);
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(" " + ex.Message);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }
        }

        #region [Shell-Code Functions]
        /// <summary>
        /// Exit the shell
        /// </summary>
        /// <param name="FunctionName">Name of the called function</param>
        /// <param name="Parameter">Given parameter</param>
        /// <returns>Empty object</returns>
        internal static void ExitShell()
        {
            runShell = false;
        }
        #endregion

        /// <summary>
        /// Print not found functions to the console
        /// </summary>
        /// <param name="Name">Name of the function wich was not found</param>
        private static void PSLHostFuncNotValidEventHandler(string Name)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("  " + Name + " not found!", false);
            Console.ForegroundColor = ConsoleColor.Gray;
        }
    }
}
